// 如果打开页面时,没有获取到启动信息,则先获取启动信息,才能去请求页面
import store from "../store"
import ajax from './ajax.js'
import config from '../projectInfo.js'
import compareJsons from './compareJsons.js'
import fileManage from './fileManage.js'

let fs = fileManage.fs,ifFileManage = fileManage.ifFileManage,userDataPath = fileManage.userDataPath;
export default (pageCode,pageTypeCode,noLoading) => {
	return new Promise((resolve, reject) => {
		if (!store.state.$prevLoaded && !store.state.$editMode && config.development === 'prod'){
			if(!store.state.$pageList.some(page=>page.pageCode===pageCode)){
				uni.showToast({
					title: '未获取到页面数据',
					icon: 'none'
				});
				reject()
			}else{
				if(!noLoading){
					uni.showLoading({
						title: '加载中'
					});
				}
				var nowPages = store.state.$pageList.filter(page=>page.pageCode===pageCode)
				var fileUrl = nowPages[0].fileUrl
				if(ifFileManage){
					// 采用文件管理
					new Promise((resolve,reject)=>{
						compareJsons(nowPages).then(()=>{
							fs.readFile({
								filePath: `${userDataPath}/${pageCode}.json`,
								encoding: 'utf8',
								success(res) {
									console.log('readFile-success',pageCode+'.json');
									resolve(JSON.parse(res.data))
								},
								fail(res) {
									console.log('readFile-fail', res)
									reject()
								}
							})
						})
					}).then(page=>{
						resolve(page)
					},()=>{
						setTimeout(()=>{
							uni.showToast({
								title: '未获取到页面数据',
								icon: 'none'
							});
						})
						reject()
					}).finally(() => {
						uni.hideLoading();
					})
				}else{
					// 不采用文件管理：兼容方法
					ajax({
						url: fileUrl,
						ifFile: true,
						method: 'get'
					}).then(res => {
						if(res.cid === pageCode){
							resolve(res)
						}else{
							setTimeout(()=>{
								uni.showToast({
									title: '未获取到页面数据',
									icon: 'none'
								});
							})
							reject()
						}
					}).catch(()=>{
						setTimeout(()=>{
							uni.showToast({
								title: '未获取到页面数据',
								icon: 'none'
							});
						})
						reject()
					}).finally(() => {
						uni.hideLoading();
					})
				}
			}
		}else{
			var ajaxData = {
				withConfig: 1,
				currentPage: 0,
				pageSize: 1,
				tag: store.state.$tag,
				version: store.state.$version,
				pageTypeCode,
				pageCode
			}
			if(store.state.$prevMode){
				ajaxData.t = new Date().getTime()
			}
			if(!noLoading){
				uni.showLoading({
					title: '加载中'
				});
			}
			ajax({
				url: '/getPageList',
				moduleUrl: true,
				noErrorMsg: true,
				method: 'get',
				data: ajaxData
			}).then(res => {
				if (res.code == 200 && res.data.length > 0) {
					var page = JSON.parse(res.data[0].page_config);
					resolve(page)
				}else{
					setTimeout(()=>{
						uni.showToast({
							title: '未获取到页面数据',
							icon: 'none'
						});
					})
					reject()
				}
			}).catch(()=>{
				setTimeout(()=>{
					uni.showToast({
						title: '未获取到页面数据',
						icon: 'none'
					});
				})
				reject()
			}).finally(() => {
				uni.hideLoading();
			})
		}
	})
}
